package day9;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Homework {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        demo4();
    }
    //a.一球从100米高度自由落下，每次落地后反跳回原高度的一半；
    //再落下，求它在第10次落地时，共经过多少米？第10次反弹多高？
    //通过线程模拟这个过程
    public static void demo1() throws ExecutionException, InterruptedException {
        FutureTask<double[]> ft=new FutureTask<>(()->{
            double[] result=new double[2];
            result[0]=100;
            result[1]=100;
            for(int i=1;i<10;i++){
                result[1]/=2;
                result[0]+=result[1]*2;
            }
            result[1]/=2;
            return result;
        });
        new Thread(ft).start();

        double[] result=ft.get();
        DecimalFormat df=new DecimalFormat("#.##");
        System.out.println("总米数:"+df.format(result[0]));
        System.out.println("反弹多高:"+df.format(result[1]));
    }
    //b.通过线程模拟对同一个变量进行更改,
    //   A线程睡眠10毫秒，对变量加1，B线程睡眠20毫秒对变量加1，持续3秒
    //   查看多次运行的结果是否一致
    public static void demo2(){
        new Demo2Thread("A").start();
        new Demo2Thread("B").start();
    }
    //a.有三个线程A，B，C，每个线程只打印线程名
    // 保证三个线程的执行顺序永远是 A -->B-->C
    public static void demo3(){
        Thread a=new Thread(()->{
            System.out.println(Thread.currentThread().getName());
        },"A");
        Thread b=new Thread(()->{
            //等待a执行结束再执行当前的B
            try {
                a.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"B");
        Thread c=new Thread(()->{
            //等待b执行结束再执行当前的c
            try {
                b.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"C");

        a.start();
        b.start();
        c.start();
    }
    //b.通过多线程模拟两人爬山，每个线程表示一个人，
    //可以设置爬山速度  每爬完100米显示信息 先到终点
    //的是胜利者， 如果有人已经到达重点了 另一个人则停止爬山
    public static void demo4(){
        Climbing cli=new Climbing(1000);
        ClimbThread a=new ClimbThread(cli,300,"A");
        ClimbThread b=new ClimbThread(cli,400,"B");
        ClimbThread c=new ClimbThread(cli,500,"C");
        a.start();
        b.start();
        c.start();
    }
}
class Climbing{//爬山对象
    int sum;//总长
    boolean win;//false
    public Climbing(int sum){
        this.sum=sum;
    }
}
class ClimbThread extends Thread{
    Climbing c;
    int speed;
    public ClimbThread(Climbing c,int speed,String name){
        super(name);
        this.speed=speed;
        this.c=c;
    }
    public void run() {
        int sum=0;
        String name=Thread.currentThread().getName();
        while(true){
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum+=100;
            System.out.println(name+"已经爬了"+sum+"米,剩余"+(c.sum-sum)+"米");
            if(sum>=c.sum){
                System.out.println(name+"已经达到终点比赛结束");
                c.win=true;
            }
            if(c.win) break;
        }
    }
}


class Demo2Thread extends Thread{
    static int n;
    public Demo2Thread(String name){
        super(name);
    }
    public void run() {
        try {
            long start=System.currentTimeMillis();
            String name=Thread.currentThread().getName();
            while(true) {
                long end=System.currentTimeMillis();
                if(end-start>=3000) break;
                if ("A".equals(name)) {
                    Thread.sleep(10);
                    n++;//n=n+1  先读取n的值 再做n+1运算 最后把结果给n赋值
                } else {
                    Thread.sleep(20);
                    n++;
                }
                System.out.println(name+"进行n变量更新:"+n);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}